package sis.dbtest;/*
* @auther 顶风少年 
* @mail dfsn19970313@foxmail.com
* @date 2019-07-30 09:50
* @notify 
* @version 1.0
*/

import junit.framework.TestCase;
import sis.db.KeyFile;
import sis.util.TestUtil;

import java.io.IOException;

public class KeyFileTest extends TestCase {
    //文件名
    private static final String FILENAME = "keyfiletest.idx";
    //索引的 key
    private static final String KEY = "key";
    //索引的值
    private static final long POSITION = 1;
    //索引的值
    private static final int LENGTH = 100;

    //索引对象
    private KeyFile keyFile;

    //初始化
    protected  void setUp()throws IOException{
        //删除文件
        TestUtil.delete(FILENAME);
        //创建文件
        keyFile = new KeyFile(FILENAME);
    }


    protected void tearDown()throws IOException{
        //删除文件
        TestUtil.delete(FILENAME);
        //重新写入数据
        keyFile.close();
    }

    //测试初始长度
    public void testCreate(){
        assertEquals(0,keyFile.size());
    }

    //测试添加
    public void testAddEntry(){
        keyFile.add(KEY,POSITION,LENGTH);
        assertEquals(1,keyFile.size());
        assertTrue(keyFile.containsKey(KEY));
        assertEquals(POSITION,keyFile.getPosition(KEY));
        assertEquals(LENGTH,keyFile.getLength(KEY));
    }

    //关闭流,在打开流,测试是否持久化数据
    public void testReopen()throws IOException{
        //添加一个对象
        keyFile.add(KEY,POSITION,LENGTH);
        //关闭流
        keyFile.close();
        //重新创建 索引 测试 是否将 文件中的数据写会 map
        keyFile = new KeyFile(FILENAME);
        assertEquals(1,keyFile.size());
        assertEquals(POSITION,keyFile.getPosition(KEY));
        assertEquals(LENGTH,keyFile.getLength(KEY));
    }
}
